﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Transactions;
using System.Text;
using System.Threading.Tasks;
using DWEIP.Models.MIS;
using DWEIP.Models;
using DWEIP.Common;
using DWEIP.BLL.Core;
using DWEIP.Locale;
using DataWin.Core.Model;
using DataWin.Core.Util;

namespace DWEIP.BLL.MIS
{
    public partial class MIS_WebIM_CommonTalkBLL : BasicBLL
    {
        #region DAL
        public IQueryable<MIS_WebIM_CommonTalk> DAL_GetList()
        {
            Table _MIS_WebIM_CommonTalk = new Table("MIS_WebIM_CommonTalk");
            _MIS_WebIM_CommonTalk.SQL.Value = @"
SELECT * FROM MIS_WebIM_Message";
            BLLSession.Load(_MIS_WebIM_CommonTalk);

            var msgModelList = (from r in _MIS_WebIM_CommonTalk
                                select new MIS_WebIM_CommonTalk
                                {
                                    Id = r["Id"].AsString,
                                    Talk = r["Talk"].AsString,
                                    State = r["State"].AsBoolean,
                                    CreateTime = r["CreateTime"].AsDateTime
                                }).ToList().AsQueryable();

            return msgModelList;
        }

        public IQueryable<MIS_WebIM_CommonTalk> DAL_GetList(Expression<Func<MIS_WebIM_CommonTalk, bool>> whereLambda)
        {
            Table _MIS_WebIM_CommonTalk = new Table("MIS_WebIM_CommonTalk");
            _MIS_WebIM_CommonTalk.SQL.Value = @"
SELECT * FROM MIS_WebIM_Message";
            BLLSession.Load(_MIS_WebIM_CommonTalk);

            var msgModelList = (from r in _MIS_WebIM_CommonTalk
                                select new MIS_WebIM_CommonTalk
                                {
                                    Id = r["Id"].AsString,
                                    Talk = r["Talk"].AsString,
                                    State = r["State"].AsBoolean,
                                    CreateTime = r["CreateTime"].AsDateTime
                                }).ToList().AsQueryable();

            return msgModelList.Where(whereLambda).AsQueryable();
        }

        public MIS_WebIM_CommonTalk DAL_GetById(string Id)
        {
            Table _MIS_WebIM_CommonTalk = new Table("MIS_WebIM_CommonTalk");
            _MIS_WebIM_CommonTalk.SQL.Value = @"
SELECT * FROM MIS_WebIM_Message Where Id=@id";
            _MIS_WebIM_CommonTalk.Params.Clear();
            _MIS_WebIM_CommonTalk.Params.Add("id", new Data(Id));

            BLLSession.Load(_MIS_WebIM_CommonTalk);

            var msgModelList = (from r in _MIS_WebIM_CommonTalk
                                select new MIS_WebIM_CommonTalk
                                {
                                    Id = r["Id"].AsString,
                                    Talk = r["Talk"].AsString,
                                    State = r["State"].AsBoolean,
                                    CreateTime = r["CreateTime"].AsDateTime
                                }).ToList();

            return msgModelList[0];
        }

        public int DAL_Delete(string Id)
        {
            Table _MIS_WebIM_CommonTalk = new Table("MIS_WebIM_CommonTalk");
            _MIS_WebIM_CommonTalk.SQL.Value = @"
DELETE MIS_WebIM_Message Where Id=@id";
            _MIS_WebIM_CommonTalk.Params.Clear();
            _MIS_WebIM_CommonTalk.Params.Add("id", new Data(Id));

            int result = BLLSession.Execute(_MIS_WebIM_CommonTalk, System.Data.CommandType.Text);
            return result;
        }

        public int DAL_Delete(string[] IdCollection)
        {
            StringBuilder sqltxt = new StringBuilder();
            foreach (string id in IdCollection)
            {
                if (sqltxt.Length>0)
                {
                    sqltxt.AppendFormat(",");
                }
                sqltxt.AppendFormat(" '{0}' ", id);
            }

            Table _MIS_WebIM_CommonTalk = new Table("MIS_WebIM_CommonTalk");
            _MIS_WebIM_CommonTalk.SQL.Value = @"
DELETE MIS_WebIM_Message Where Id in (@id)";
            _MIS_WebIM_CommonTalk.Params.Clear();
            _MIS_WebIM_CommonTalk.Params.Add("id", new Data(sqltxt.ToString()));

            int result = BLLSession.Execute(_MIS_WebIM_CommonTalk, System.Data.CommandType.Text);
            return result;
        }

        public bool DAL_Create(MIS_WebIM_CommonTalk model)
        {
            string sqltxt = @"INSERT INTO MIS_WebIM_CommonTalk VALUES('{0}', '{1}', {2}, '{3}')";

            Table _MIS_WebIM_CommonTalk = new Table("MIS_WebIM_CommonTalk");
            _MIS_WebIM_CommonTalk.SQL.Value = string.Format(sqltxt, 
                model.Id, 
                model.Talk,
                (model.State??false)?1:0,
                model.CreateTime
                );

            int result = BLLSession.Execute(_MIS_WebIM_CommonTalk, System.Data.CommandType.Text);
            return result > 0;
        }

        public bool DAL_Edit(MIS_WebIM_CommonTalk model)
        {
            string sqltxt = @"UPDATE MIS_WebIM_CommonTalk SET Talk='{1}', Sate={2}, CreateTime='{3}' WHERE Id='{0}'";

            Table _MIS_WebIM_CommonTalk = new Table("MIS_WebIM_CommonTalk");
            _MIS_WebIM_CommonTalk.SQL.Value = string.Format(sqltxt,
                model.Id,
                model.Talk,
                (model.State ?? false) ? 1 : 0,
                model.CreateTime
                );

            int result = BLLSession.Execute(_MIS_WebIM_CommonTalk, System.Data.CommandType.Text);
            return result > 0;
        }

        public bool DAL_IsExist(string id)
        {
            return DAL_GetById(id) != null;
        }

        #endregion DAL


        public List<MIS_WebIM_CommonTalkModel> GetList(ref GridPager pager, string queryStr)
        {

            IQueryable<MIS_WebIM_CommonTalk> queryData = null;
            if (!string.IsNullOrWhiteSpace(queryStr))
            {
                queryData = DAL_GetList(
                                a => a.Id.Contains(queryStr)
                                || a.Talk.Contains(queryStr)


                                );
            }
            else
            {
                queryData = DAL_GetList();
            }
            pager.totalRows = queryData.Count();
            //排序
            queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
            return CreateModelList(ref queryData);
        }
        public List<MIS_WebIM_CommonTalkModel> CreateModelList(ref IQueryable<MIS_WebIM_CommonTalk> queryData)
        {

            List<MIS_WebIM_CommonTalkModel> modelList = (from r in queryData
                                                         select new MIS_WebIM_CommonTalkModel
                                                         {
                                                             Id = r.Id,
                                                             Talk = r.Talk,
                                                             State = r.State,
                                                             CreateTime = r.CreateTime,

                                                         }).ToList();

            return modelList;
        }

        public bool Create(ref ValidationErrors errors, MIS_WebIM_CommonTalkModel model)
        {
            try
            {
                MIS_WebIM_CommonTalk entity = DAL_GetById(model.Id);
                if (entity != null)
                {
                    errors.Add(Resource.PrimaryRepeat);
                    return false;
                }
                entity = new MIS_WebIM_CommonTalk();
                entity.Id = model.Id;
                entity.Talk = model.Talk;
                entity.State = model.State;
                entity.CreateTime = model.CreateTime;


                if (DAL_Create(entity))
                {
                    return true;
                }
                else
                {
                    errors.Add(Resource.InsertFail);
                    return false;
                }
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }



        public bool Delete(ref ValidationErrors errors, string id)
        {
            try
            {
                if (DAL_Delete(id) == 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

        public bool Delete(ref ValidationErrors errors, string[] deleteCollection)
        {
            try
            {
                if (deleteCollection != null)
                {
                    using (TransactionScope transactionScope = new TransactionScope())
                    {
                        if (DAL_Delete(deleteCollection) == deleteCollection.Length)
                        {
                            transactionScope.Complete();
                            return true;
                        }
                        else
                        {
                            Transaction.Current.Rollback();
                            return false;
                        }
                    }
                }
                return false;
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }




        public bool Edit(ref ValidationErrors errors, MIS_WebIM_CommonTalkModel model)
        {
            try
            {
                MIS_WebIM_CommonTalk entity = DAL_GetById(model.Id);
                if (entity == null)
                {
                    errors.Add(Resource.Disable);
                    return false;
                }
                entity.Id = model.Id;
                entity.Talk = model.Talk;
                entity.State = model.State;
                entity.CreateTime = model.CreateTime;



                if (DAL_Edit(entity))
                {
                    return true;
                }
                else
                {
                    errors.Add(Resource.NoDataChange);
                    return false;
                }

            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }



        public MIS_WebIM_CommonTalkModel GetById(string id)
        {
            if (IsExists(id))
            {
                MIS_WebIM_CommonTalk entity = DAL_GetById(id);
                MIS_WebIM_CommonTalkModel model = new MIS_WebIM_CommonTalkModel();
                model.Id = entity.Id;
                model.Talk = entity.Talk;
                model.State = entity.State;
                model.CreateTime = entity.CreateTime;

                return model;
            }
            else
            {
                return null;
            }
        }

        public bool IsExists(string id)
        {
            return DAL_IsExist(id);
        }
        public void Dispose()
        {

        }
    }
}
